package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.course.domain.TCourse;
import com.ruoyi.course.vo.TCourseVo;
import com.ruoyi.course.mapper.TCourseMapper;
import com.ruoyi.course.service.ITCourseService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class TCourseServiceImpl extends ServiceImpl<TCourseMapper, TCourse> implements ITCourseService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public TableDataInfo<TCourseVo> queryList(TCourseVo entity) {
        Page<TCourseVo> tCourseVoPage = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        List<TCourseVo> records = tCourseVoPage.getRecords();
        for (TCourseVo record : records) {
//            查询用户名
            String username = null;
            LambdaQueryWrapper<SysUser> userqw = new LambdaQueryWrapper<>();
            userqw.eq(SysUser::getUserId,record.gettId());
            SysUser sysUser = sysUserMapper.selectOne(userqw);
            if (sysUser!=null){
                username = sysUser.getNickName();
            }
            record.setTeacherName(username);
        }
        return PageUtils.buildDataInfo(tCourseVoPage);
    }

    @Override
    public List<TCourseVo> queryAll(TCourseVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TCourseVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }
}
